﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace GameJam
{
	[RequireComponent(typeof(SpriteRenderer))]
	public class EnergyPoint : MonoBehaviour 
	{
		bool _bCouldAttach = true;
		Animator _animator;

		void Start ()
		{
			_animator = this.GetComponent<Animator> ();
		}

		void Update () 
		{
			
		}

		void OnTriggerEnter2D(Collider2D other)
		{
			if (other.gameObject.tag == "Player") 
			{
				TreePlayer player = other.gameObject.GetComponent<TreePlayer> ();

				if (player.IsEnergyFull)
					return;
				
				if (EnergySpawner.Instance != null) 
				{
					StartCoroutine (delayDespawn ());
					player.AddEnergy ();
				}
			}
		}


		IEnumerator delayDespawn()
		{
			_animator.Play ("eaten");
			yield return new WaitForSeconds (2.0f);
			EnergySpawner.Instance.Despawn (this);
		}
	}
}
